const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const wxapp_id = 1;
// const baseUrl = "http://localhost:8081/";
// const baseUrl = "http://pdd.com/";
const baseUrl = "https://pdd.allfighting.top/";
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
//其他参数可以自定义传入
function requestAjax(url, postData, success, fail, message = '加载中') {
    // checkLogin();
    // console.log(data)
    wx.showNavigationBarLoading()
    if (message != "") {
        wx.showLoading({
            title: message,
        })
    }
    let token = wx.getStorageSync('XX-Token');
    wx.request({
        //可以写上请求的域名  后期改测试服正式服 改一个地方就可以 前缀写上后期上线改地址好改
        url: baseUrl + url,
        // url='/index/video'
        data: postData,
        header: {
            //'Content-Type': 'application/json' 默认
            'content-type': 'application/x-www-form-urlencoded',
            'XX-Token': token,//根据自己的接口写header的传参
            'XX-Device-Type': 'wxapp'
            //'Logintime': logintime
        },
        method: 'post',//方法也可以改成变量 传入
        success: function (res) {
            //console.log(res.data)
            wx.hideNavigationBarLoading()
            if (message != "") {
                wx.hideLoading()
            }
            if (res.statusCode == 200) {
                success(res.data)
            } else {
                fail()
            }

        },
        fail: function (res) {
            wx.hideNavigationBarLoading()
            if (message != "") {
                wx.hideLoading()
            }
            fail()
        },
        complete: function (res) {

        },
    })
}
function sendRequest(url, params) {   //同步封装
    var that = this;
    wx.showNavigationBarLoading()
    wx.showLoading({
        title: '加载中',
    })
    let token = wx.getStorageSync('XX-Token');
    return new Promise(function (resolve, reject) {
        wx.request({
            url: baseUrl + url,
            data: params,
            method: "POST",
            header: {
                "Content-Type": "application/json",
                'token': token,//根据自己的接口写header的传参
                'XX-Device-Type': 'wxapp',
                'wxapp-id':wxapp_id
            },
            success: function (res) {
                resolve(res.data);
                wx.hideNavigationBarLoading()
                wx.hideLoading()
            },
            fail: function (res) {
                reject(res);
            },
        })
    });
}

function postDataLogin(url, param, success) {
    sendRequest(url, param).then(function (result) {
        if (result.code == -10010) {
            wx.login({
                success: res => {
                    sendRequest("index/login/login", { code: res.code }).then(function (res) {
                        if (res.code > 0) {
                            wx.setStorageSync('XX-Token', res.data.token);
                            wx.setStorageSync("is_login", 1)
                            wx.setStorageSync("userinfo", res.data.userinfo)
                            sendRequest(url, param).then(success);
                        }
                    });
                }
            })
        } else {
            success(result)
        }
    })
}
module.exports = {
    formatTime: formatTime,
    requestAjax: requestAjax,
    sendRequest: sendRequest,
    postDataLogin: postDataLogin
}
